package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * 定时清空无效缓存图片
 */
public class ClearImgJob {
    @Autowired
    private JedisPool jedisPool;//注入javaredis客户端连接redis服务器的地址池对象

    public void clearImg() {
        //根据redis缓存的俩个set集合获取缓存的垃圾图片
        Set<String> sdiff = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (sdiff != null) {
            for (String s : sdiff) {
                //从七牛云远程服务器删除无效的图片
                QiniuUtils.deleteFileFromQiniu(s);
                //将redis集合的无效缓存图片路径删除
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES, s);
            }
        }
    }
}
